package com.hk.yajin.function.members.activity.setting;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.hk.developmentlib.baseben.NewWorkBean;
import com.hk.developmentlib.callback.HttpCallBack;
import com.hk.yajin.R;
import com.hk.yajin.base.BaseActivity;
import com.hk.yajin.function.MainActivity;
import com.hk.yajin.function.members.activity.LoginAndRegisterActivity;
import com.hk.yajin.manager.UserInfoManager;
import com.hk.yajin.utills.HttpRequestUtils;
import com.hk.yajin.utills.url.UrlConstants;

import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ChangePasswordActivity extends BaseActivity {


    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tv_old)
    EditText tvOld;
    @BindView(R.id.tv_new)
    EditText tvNew;
    @BindView(R.id.tv_new2)
    EditText tvNew2;
    @BindView(R.id.tv_sure)
    TextView tvSure;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password);
        ButterKnife.bind(this);
        intiView();
    }

    private void intiView() {
        initToolBar(R.string.changepassword);
    }


    @OnClick(R.id.tv_sure)
    public void onViewClicked() {
        String oldPwd = tvOld.getText().toString();
        String newPwd = tvNew.getText().toString();
        String newPwd2 = tvNew2.getText().toString();
        if (TextUtils.isEmpty(oldPwd) || TextUtils.isEmpty(newPwd) || TextUtils.isEmpty(newPwd2)) {
            showShortToast("请输入完成再提交");
            return;
        }

        if (!newPwd.equals(newPwd2)) {
            showShortToast("两次输入的新密码不一致");
            return;
        }

        if (!newPwd.equals(newPwd2)) {
            showShortToast("两次输入的新密码不一致");
            return;
        }

        if (isNetWorkConnect()) {
            showLoadingView();
            Map<String, Object> reqParam = UserInfoManager.getUserKeyMapIfLogin();
            reqParam.put("old_pwd", oldPwd);
            reqParam.put("new_pwd", newPwd);

            HttpRequestUtils.post(false, UrlConstants.FUNCTION_UPDATE_PWD, reqParam, false, new HttpCallBack() {
                @Override
                public void onSucced(String data) {
                    hideLoadingView();
                    showShortToast("设置成功");
                    UserInfoManager.getInstance().exitLogin();
                    MainActivity.starItSelf(ChangePasswordActivity.this,MainActivity.class);
                    startActivity(LoginAndRegisterActivity.class);
                }

                @Override
                public void onErrorCode(NewWorkBean bean) {
                    hideLoadingView();
                    showShortToast(bean.getMsg());
                }

                @Override
                public void onBadHttp(String tip) {
                    hideLoadingView();
                    showShortToast(tip);
                }

            });
        }
    }
}
